﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.ObjectModel;
using Eassets.Layered.Domain.Global;


namespace Eassets.Data.Global
{
    public class TransactionTypeData
    {
        /// <summary>
        /// Inserts transactionType.
        /// </summary>
        /// <param name="transactionType"></param>
        public void AddTransactionType(TransactionType transactionType)
        {
            SqlCommand command = new SqlCommand("SP_AddTransactionType"); // Name of the SP
            CommonData commonData = new CommonData(); commonData.ExecuteNonQuery(ConstructCommand(command, transactionType));
        }

        /// <summary>
        /// Updates transactionType.
        /// </summary>
        /// <param name="transactionType"></param>
        public void ModifyTransactionType(TransactionType transactionType)
        {
            SqlCommand command = new SqlCommand("SP_ModifyTransactionType"); // Name of the SP
            command.Parameters.AddWithValue("@TransactionTypeId", transactionType.TransactionTypeId);
            CommonData commonData = new CommonData(); commonData.ExecuteNonQuery(ConstructCommand(command, transactionType));
        }


        /// <summary>
        /// Delete transactionType.
        /// </summary>
        /// <param name="transactionTypeId"></param>
        public void DeleteTransactionType(long transactionTypeId)
        {
            SqlCommand command = new SqlCommand("SP_DeleteTransactionType"); // Name of the SP
            command.Parameters.AddWithValue("@TransactionTypeId", transactionTypeId);
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData();
            commonData.ExecuteNonQuery(command);
        }

        /// <summary>
        /// GetbyId transactionType.
        /// </summary>
        /// <param name="transactionTypeId"></param>
        /// <returns></returns>
        public TransactionType GetByIdTransactionType(long transactionTypeId)
        {
            SqlCommand command = new SqlCommand("SP_GetTransactionTypeById");
            command.Parameters.AddWithValue("@TransactionTypeId", transactionTypeId);
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData(); SqlDataReader dataReader = commonData.ExecuteReader(command);
            TransactionType transactionType = new TransactionType();
            while (dataReader.Read())
            {
                if (!(String.IsNullOrEmpty(dataReader["TransactionTypeId"].ToString())))
                {
                    transactionType.TransactionTypeId = Convert.ToInt64(dataReader["TransactionTypeId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["Code"].ToString())))
                {
                    transactionType.Code = Convert.ToString(dataReader["Code"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["Description"].ToString())))
                {
                    transactionType.Description = Convert.ToString(dataReader["Description"]);
                }
            }
            dataReader.Close();
            return transactionType;
        }

        /// <summary>
        /// GetAll transactionType.
        /// </summary>
        /// <returns></returns>
        public Collection<TransactionType> GetAllTransactionType()
        {
            Collection<TransactionType> transactionTypes = new Collection<TransactionType>();
            SqlCommand command = new SqlCommand("SP_GetAllTransactionTypes");
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData(); SqlDataReader dataReader = commonData.ExecuteReader(command);
            TransactionType transactionType;
            while (dataReader.Read())
            {
                transactionType = new TransactionType();
                if (!(String.IsNullOrEmpty(dataReader["TransactionTypeId"].ToString())))
                {
                    transactionType.TransactionTypeId = Convert.ToInt64(dataReader["TransactionTypeId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["Code"].ToString())))
                {
                    transactionType.Code = Convert.ToString(dataReader["Code"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["Description"].ToString())))
                {
                    transactionType.Description = Convert.ToString(dataReader["Description"]);
                }

                transactionTypes.Add(transactionType);
            }
            dataReader.Close();
            return transactionTypes;
        }

        public SqlCommand ConstructCommand(SqlCommand command, TransactionType transactionType)
        {
            command.CommandType = CommandType.StoredProcedure;

            try
            {
                command.Parameters.AddWithValue("@Code", transactionType.Code);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@Description", transactionType.Description);
            }
            catch (Exception)
            { }
            return command;
        }
    }
}

